Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exported types #239

Merged
merged 3 commits into from
Sep 15, 2022
Merged

fix exported types #239

merged 3 commits into from
Sep 15, 2022

Conversation

jjenzz
Copy link
Contributor

@jjenzz jjenzz commented Sep 14, 2022

Fixes issue mentioned here #237 (comment)

The styled.d.ts definition was trying to manually construct the Props type and looks like it was getting it wrong somewhere. Exporting the type will ensure it uses import("./create-themed-component").Props correctly instead.

DIFF

export declare function styled<
  C extends ComponentType<any>,
  ThemeKey extends keyof DripsyFinalTheme = keyof DripsyFinalTheme
>(
  Component: C,
  {
    themeKey,
    defaultVariant,
  }?: Pick<ThemedOptions<any, ThemeKey>, 'themeKey' | 'defaultVariant'>
): <Extra>(
  defaultStyle?:
    | import('./types').Sx
    | ((props: Extra) => import('./types').Sx)
    | undefined
) => import('react').ForwardRefExoticComponent<
  import('react').PropsWithoutRef<
    import('react').PropsWithChildren<
-      C extends ComponentType<infer BaseProps>
-        ? Omit<
-            {
-              [P in Exclude<
-                keyof BaseProps,
-                'variant' | 'variants'
-              >]: BaseProps[P]
-            },
-            keyof import('./types').StyledProps<ThemeKey_1> | keyof Extra
-          > &
-            Extra &
-            import('./types').StyledProps<ThemeKey>
-        : never
+      import('./create-themed-component').Props<C, Extra, ThemeKey>
    >
  > &
    import('react').RefAttributes<import('react').ElementRef<C>>
 >

@vercel
Copy link

vercel bot commented Sep 14, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
dripsy ❌ Failed (Inspect) Sep 15, 2022 at 2:22PM (UTC)

@jjenzz jjenzz marked this pull request as draft September 14, 2022 21:39
@jjenzz jjenzz marked this pull request as ready for review September 14, 2022 21:41
@nandorojo
Copy link
Owner

Hmm but this still doesn't allow the styled<{ myProp: boolean }>() syntax, right?

@jjenzz
Copy link
Contributor Author

jjenzz commented Sep 14, 2022

oh, i didn't realise that was an intended api 😬 my bad.

what is the expectation if someone does styled<{ test: 'foo' }>(View)<{ test: 'bar' }>(); ?

@jjenzz
Copy link
Contributor Author

jjenzz commented Sep 14, 2022

i'll have to take a look another time as it is super late here now, sorry.

unfortunately, the C generic would need to be optional for that API to work and i don't know if it is possible to infer C if a type is passed for Props. doing so will cause C to fall back to its default value (instead of inference): microsoft/TypeScript#43119

// would infer correctly
const a = styled(Text)()

// inference would break because `C` would fallback to its default value (likely `ComponentType<any>`)
const b = styled<{ prop?: boolean }>(Text)();

my solution relies on being able to infer the Text component type here in order to get its ElementRef and props 😞

the best solution for now might be to revert my change. or would you consider deprecating the broken api?

@jjenzz
Copy link
Contributor Author

jjenzz commented Sep 15, 2022

@nandorojo funny what a bit of sleep can do.

this should all be working now assuming the following tests look okay to you? note that when using styled<Props>(Comp)() api you must include Comp props as part of the Props generic (matches behavior in v3.7.4).

export const ExtendedCompWithExtraProps = styled<
  React.ComponentProps<typeof KeyboardAvoidingView> & { custom?: boolean }
>(KeyboardAvoidingView)({})

/* @ts-expect-error behavior cannot be 'test' */
export const ExtendE = () => <ExtendedCompWithExtraProps behavior="test" />
/* should not error */
export const ExtendF = () => <ExtendedCompWithExtraProps behavior="padding" />
/* @ts-expect-error custom cannot be string */
export const ExtendG = () => <ExtendedCompWithExtraProps custom="test" />
/* should not error */
export const ExtendH = () => <ExtendedCompWithExtraProps custom={false} />

@jjenzz jjenzz marked this pull request as ready for review September 15, 2022 08:16
Comment on lines 145 to 176
export const ExtendedComp = styled(KeyboardAvoidingView)({})

export const ExtendedCompWithExtraProps = styled<
React.ComponentProps<typeof KeyboardAvoidingView> & { custom?: boolean }
>(KeyboardAvoidingView)({})

export const ExtendedCompWithExtraProps2 = styled(KeyboardAvoidingView)<
React.ComponentProps<typeof KeyboardAvoidingView> & { custom?: boolean }
>({})

/* @ts-expect-error behavior cannot be 'test' */
export const ExtendA = () => <ExtendedComp behavior="test" />
/* should not error */
export const ExtendB = () => <ExtendedComp behavior="padding" />
/* @ts-expect-error custom does not exist */
export const ExtendC = () => <ExtendedComp custom="test" />
/* @ts-expect-error behavior cannot be 'test' */
export const ExtendE = () => <ExtendedCompWithExtraProps behavior="test" />
/* should not error */
export const ExtendF = () => <ExtendedCompWithExtraProps behavior="padding" />
/* @ts-expect-error custom cannot be string */
export const ExtendG = () => <ExtendedCompWithExtraProps custom="test" />
/* should not error */
export const ExtendH = () => <ExtendedCompWithExtraProps custom={false} />
/* @ts-expect-error behavior cannot be 'test' */
export const ExtendI = () => <ExtendedCompWithExtraProps2 behavior="test" />
/* should not error */
export const ExtendJ = () => <ExtendedCompWithExtraProps2 behavior="padding" />
/* @ts-expect-error custom cannot be string */
export const ExtendK = () => <ExtendedCompWithExtraProps2 custom="test" />
/* should not error */
export const ExtendL = () => <ExtendedCompWithExtraProps2 custom={false} />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like all this type testing should belong somewhere else other than examples/example/App.tsx... some kind of tests.tsx file somewhere. let me know if you'd like me to move it all.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this should get cleaned up at some point. examples isn't a great name for this, it's really just for local dev testing. i point people to the solito starter instead for using dripsy

@nandorojo
Copy link
Owner

this behavior looks right! i'll take a look today

@jjenzz
Copy link
Contributor Author

jjenzz commented Sep 15, 2022

@nandorojo great, thanks ❤️

also, just a heads up that for some reason installing older versions of dripsy installs the latest versions of types so my project has errors everywhere that i cannot silence with a downgrade 🙈

not sure why older versions don't pull types specific to that version but something for you to look into separately perhaps?

@nandorojo
Copy link
Owner

hmm it could be something related to the monorepo setup perhaps...not sure. hopefully publishing the newer version will fix it all

@nandorojo
Copy link
Owner

if you also have gradient installed, be sure to fix it at the same version. i'm going to fix this in v4

@jjenzz
Copy link
Contributor Author

jjenzz commented Sep 15, 2022

@nandorojo i think it may have been something to do with local caching (pnpm maybe 🤷 ). i've completely removed a bunch of stuff and reinstalled 3.6.0 and have the correct types, so that's a relief!

@nandorojo nandorojo merged commit c979df6 into nandorojo:master Sep 15, 2022
@jjenzz jjenzz deleted the fix-exported-types branch September 15, 2022 14:23
@nandorojo
Copy link
Owner

just published an updated version (3.8.1) with these changes

@nandorojo
Copy link
Owner

nandorojo commented Dec 26, 2022

Hey @jjenzz! I think one of these PRs caused a type regression for ScrollView and TextInput. I can't quite figure out why. My best guess is that they have some unique ref type, since both of these components have custom methods like scrollTo() and focus(). That cause might be a red herring, but it would make sense to me...

Here's the issue: #260

Thank you (& happy holidays!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants